<?php

class RedirectHandler {

	private static $instance = null;
	private $isDebug = false;

	private function __construct() {
		
	}

	public function enableDebug($isDebug) {
		$this->isDebug = (bool) $isDebug;
	}

	public function doRedirect($url, Exception $ex = null) {
		if (!$this->isDebug || $ex == null) {
			$redirector = new Zend_Controller_Action_Helper_Redirector();
			$redirector->gotoUrlAndExit($url);
			exit;
		} else {
			var_dump($ex->getMessage());
			echo '<a href="' . $url . '">Click for redirect link</a>';
			exit;
		}
	}

	public function goToReferer($ex = null) {
		$this->doRedirect($_SERVER['HTTP_REFERER'], $ex);
	}

	/**
	 *
	 * @return RedirectHandler
	 */
	public static function instance() {
		if (self::$instance == null) {
			self::$instance = new RedirectHandler();
		}

		return self::$instance;
	}

}